import type { CreateOrUpdateEnterpriseRequest, EnterpriseQuery, EnterpriseResponse } from "./type"
import { request } from "@/http/axios"
// 导入 mock 函数
import * as MockApis from "./mock"

// 判断是否使用 mock 数据（开发环境使用）
const USE_MOCK = import.meta.env.DEV

/** 获取企业列表 */
export function getEnterpriseListApi(params: EnterpriseQuery) {
  if (USE_MOCK) {
    return MockApis.getEnterpriseListApiMock(params)
  }
  return request<EnterpriseResponse>({
    url: "internship/enterprise",
    method: "get",
    params
  })
}

/** 创建企业 */
export function createEnterpriseApi(data: CreateOrUpdateEnterpriseRequest) {
  if (USE_MOCK) {
    return MockApis.createEnterpriseApiMock(data)
  }
  return request({
    url: "internship/enterprise",
    method: "post",
    data
  })
}

/** 更新企业 */
export function updateEnterpriseApi(data: CreateOrUpdateEnterpriseRequest) {
  if (USE_MOCK) {
    return MockApis.updateEnterpriseApiMock(data)
  }
  return request({
    url: "internship/enterprise",
    method: "put",
    data
  })
}

/** 更新企业状态 */
export function updateEnterpriseStatusApi(id: number, status: boolean) {
  if (USE_MOCK) {
    return MockApis.updateEnterpriseStatusApiMock(id, status)
  }
  return request({
    url: `internship/enterprise/${id}/status`,
    method: "put",
    data: { status }
  })
}

/** 上传营业执照 */
export function uploadLicenseApi(file: File) {
  if (USE_MOCK) {
    return MockApis.uploadLicenseApiMock(file)
  }
  const formData = new FormData()
  formData.append("file", file)
  return request<ApiResponseData<{ url: string }>>({
    url: "internship/enterprise/upload-license",
    method: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  })
}

/** 检查企业名称是否重复 */
export function checkEnterpriseNameApi(fullName: string, id?: number) {
  if (USE_MOCK) {
    return MockApis.checkEnterpriseNameApiMock(fullName, id)
  }
  return request<ApiResponseData<{ exist: boolean }>>({
    url: "internship/enterprise/check-name",
    method: "get",
    params: { fullName, id }
  })
}

/** 获取可用企业列表 */
export function getAvailableEnterpriseListApi() {
  if (USE_MOCK) {
    return MockApis.getAvailableEnterpriseListApiMock()
  }
  return request<ApiResponseData<Array<{ id: number, name: string }>>>({
    url: "internship/enterprise/available",
    method: "get"
  })
}
